#!/bin/bash

if [ $# -ne 2 ];then
  echo "input vm_id ip"
  exit 127
fi


vmid=100
vmip=192.168.31.220
logs_path="/var/log/vm_netcheck.log"

function main {
check_rec=`ping -w 9 ${vmip}|grep 'received'|awk -F',' '{print$2}'|awk '{print$1}'|xargs echo -n`

uptime_m=`qm status ${vmid} -verbose|grep uptime|awk '{print$NF}'|xargs echo -n`

if [ "${check_rec}" == "0" -a ${uptime_m} -gt 600 ];then
    echo "[ERROR]: `date` network error,start to reboot......" ;
    /root/bin/vmkill ${vmid} y||echo "no running......"
    sleep 5;
    echo "`date` start ${vmid}......";
    qm start ${vmid};
else
    echo "[${vmid}_CHECK_INFO]: `date` ${vmid}-${vmip} check network pass......";
fi

}

main |tee -a ${logs_path};

# */15 * * * *  /root/bin/vm_netcheck id ip